<?php

/**
 * worry actions.
 *
 * @package    jiazhang
 * @subpackage worry
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class worryActions extends sfActions
{

    
  public function executeIndex(sfWebRequest $request)
  {
        $this->getResponse()->setTitle('烦恼 - 壹童学');
        $current_week_hot = (int)$request->getParameter('week',0); 
        $has_answer = (int)$request->getParameter('answer',0);
        $my_answer_worry = (int)$request->getParameter('my_answer',0);
        $my_attention = (int)$request->getParameter('attention',0);
        $user_id = $this->getUser()->getAttribute('user_id',0);
        $my_worry = (int)$request->getParameter('my_worry',0);
        $hot_tag = (int)$request->getParameter('hot_tag',0);
        $tag_id = (int)$request->getParameter('tag',0);
        
        $dql = Doctrine::getTable('Worry')->createQuery()->where('display = 1')->orderBy('created_at desc');

        if($current_week_hot == 1){
            $this->getResponse()->setTitle('本周最热 - 烦恼 - 壹童学');
            $dql = Doctrine::getTable('Worry')->createQuery()->where('display = 1')->orderBy('created_at desc');
        }
        
        if($my_worry == 1){
            $this->getResponse()->setTitle('我的烦恼 - 烦恼 - 壹童学');
            $dql = Doctrine::getTable('Worry')->createQuery()->where('user_id',$user_id)->andWhere('display = 1');
        }
        
        if($has_answer == 1){
            $this->getResponse()->setTitle('已解决的烦恼 - 烦恼 - 壹童学');
            $worry_ids = Doctrine::getTable('WorryStatistical')->createQuery()->where('answer_num > 0')->execute()->toKeyValueArray('worry_id', 'worry_id');
            $dql = Doctrine::getTable('Worry')->createQuery()->whereIn('id',$worry_ids)->andWhere('display = 1');
        }
        
        if($has_answer == 2){
            $this->getResponse()->setTitle('未解决的烦恼 - 烦恼 - 壹童学');
            $worry_ids = Doctrine::getTable('WorryStatistical')->createQuery()->where('answer_num = 0')->execute()->toKeyValueArray('worry_id', 'worry_id');
            $dql = Doctrine::getTable('Worry')->createQuery()->whereIn('id',$worry_ids)->andWhere('display = 1');
        }
        
        if($my_answer_worry == 1){
            $this->getResponse()->setTitle('我回答的烦恼 - 烦恼 - 壹童学');
            $worry_ids = Doctrine::getTable('WorryAnswer')->createQuery()->where('user_id = ?',$user_id)->andWhere('parent_id = 0')->execute()->toKeyValueArray('worry_id', 'worry_id');

            if(count($worry_ids) != 0){
               $dql = Doctrine::getTable('Worry')->createQuery()->whereIn('id',$worry_ids)->andWhere('display = 1');
            }else{
                $dql = Doctrine::getTable('Worry')->createQuery()->where('id = 0');
            }
        }
        
        if($hot_tag == 1){
            $this->getResponse()->setTitle('热门烦恼 - 烦恼 - 壹童学');
            $worry_and_tags = Doctrine::getTable('WorryAndTag')->createQuery()->where('worry_tag_id = ?',$tag_id)->execute();
            
            if($worry_and_tags->count() == 0){
                 $dql = Doctrine::getTable('Worry')->createQuery()->where('id = 0');
            }else{
                $worry_ids = $worry_and_tags->toKeyValueArray('worry_id', 'worry_id');
                $dql = Doctrine::getTable('Worry')->createQuery()->whereIn('id',$worry_ids)->andWhere('display = 1');
            }
        }
        
        if($my_attention == 1){
            $this->getResponse()->setTitle('我关注的烦恼 - 烦恼 - 壹童学');
            $worry_ids = Doctrine::getTable('WorryAttention')->createQuery()->where('user_id = ?',$user_id)->andWhere('is_author = 0')->execute()->toKeyValueArray('worry_id', 'worry_id');
           
            if(count($worry_ids) != 0){
               $dql = Doctrine::getTable('Worry')->createQuery()->whereIn('id',$worry_ids)->andWhere('display = 1');
            }else{
                $dql = Doctrine::getTable('Worry')->createQuery()->where('id = 0');
            }
        }
        
        $page = $request->getParameter('page',1);
        $this->pager = new sfDoctrinePager('Worry',20);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
  }
  
  //解答举报
  public function executeReport(sfWebRequest $request){
      
      if($request->isXmlHttpRequest()){
          $return_data = array();
          $user_id = $this->getUser()->getAttribute('user_id',0);
          $answer_id = (int)$request->getParameter('answer_id',0);
          
          $report = new AnswerReport();
          $report->setAnswerId($answer_id)->setUserId($user_id)->save();
          $return_data['msg'] = '谢谢您的关注与监督，我们会尽快处理您的反馈';
          return $this->renderText(json_encode($return_data));
      }
  }
  
  //打赏
  public function executeCommendation(sfWebRequest $request){
      if($request->isXmlHttpRequest()){
          
          $return_data = array();
          
          if(!$this->getUser()->isAuthenticated()){
              $return_data['error'] = 1;
              $return_data['msg'] = '请先登入';
              return $this->renderText(json_encode($return_data));
          }
          
          $user_id = $this->getUser()->getAttribute('user_id',0);
          $answer_id = (int)$request->getParameter('answer_id',0);
          $answer_user_id = (int)$request->getParameter('answer_user_id',0);
          $has_commendation = Doctrine::getTable('WorryCommendation')->createQuery()->where('answer_id = ?',$answer_id)->andWhere('action_user_id  = ?',$user_id)->execute();
          
          
          if(count($has_commendation) > 0){
              $return_data['error'] = 1;
              $return_data['msg'] = '你已经打赏过了哦';
          }else{
             $commendation = new WorryCommendation();
             $commendation->setAnswerId($answer_id)->setUserId($answer_user_id)->setActionUserId($user_id)->save(); 
             $num = Doctrine::getTable('WorryAnswer')->addCommendationNum($answer_id);
             $return_data['error'] = 0;
             $return_data['msg'] = '打赏成功，谢谢你的关注';
             $return_data['num'] = $num;
          }
           return $this->renderText(json_encode($return_data));
      }
  }

  //发布评论
  public function executeComment(sfWebRequest $request){
      sfConfig::set('sf_web_debug',false);
     $user_id = $this->getUser()->getAttribute('user_id',0);
     $worry_id = (int)$request->getParameter('worry_id',0);
     $this->forward404Unless($this->worry = Doctrine::getTable('Worry')->findOneById($worry_id));
     
     if($request->isMethod('post')){
         $content = $request->getParameter('content','');
         $comment = new WorryComment();
         $comment->setUserId($user_id)->setWorryId($worry_id)->setContent($content)->save();
         $this->worry->addCommentNum();
         $this->getUser()->setFlash('success','评论发布成功！');
         $this->redirect('worry/comment?worry_id='.$worry_id);
     }
  }
  //修改烦恼
  public function executeEdit(sfWebRequest $request){
     sfConfig::set('sf_web_debug',false);
     $user_id = $this->getUser()->getAttribute('user_id',0);
     $worry_id = (int)$request->getParameter('id',0);
     $this->forward404Unless($this->worry = Doctrine::getTable('Worry')->findOneById($worry_id));
     
     if($this->worry->getUserId() != $user_id){
         return $this->renderText('您不是作者无法修改烦恼！');
     }
     
     if($request->isMethod('post')){
         $content = $request->getParameter('content','');
         $this->worry->setContent($content)->save();
         $this->getUser()->setFlash('success','烦恼保存成功！');
         $this->redirect('worry/edit?id='.$worry_id);
     }
     
  }
  
  
  //创建回复
  public function executeCreate_reply(sfWebRequest $request){
      if($request->isMethod('POST')){
          $user_id = $this->getUser()->getAttribute('user_id',0);
          $worry_id = (int)$request->getParameter('worry_id',0);
          $parent_id = (int)$request->getParameter('answer_id',0);
          $content = trim($request->getParameter('content',''));
          $worry_answer = new WorryAnswer();
          $worry_answer->setUserId($user_id)->setContent($content)->setWorryId($worry_id)->setParentId($parent_id)->save();
          $this->getUser()->setFlash('success','解答回复成功！');
          $this->redirect('worry/show?id='.$worry_id);
      }
  }
  
  //创建解答
  public function executeCreate_answer(sfWebRequest $request){
      if($request->isMethod('POST')){
          $user_id = $this->getUser()->getAttribute('user_id',0);
          $worry_id = (int)$request->getParameter('worry_id',0);
          $content = trim($request->getParameter('content',''));
          
          if(!$this->getUser()->isAuthenticated()){
              $this->getUser()->setFlash('error','请先登录');
              $this->redirect('worry/show?id='.$worry_id);
          }
          
          $this->forward404Unless($worry = Doctrine::getTable('Worry')->findOneById($worry_id));
          
          if($worry->getUserId() == $user_id){
              $this->getUser()->setFlash('error','如果自己解答了就木有烦恼啦');
              $this->redirect('worry/show?id='.$worry_id);
          }
          
          $worry_answer = new WorryAnswer();
          $worry_answer->setUserId($user_id)->setContent($content)->setWorryId($worry_id)->setParentId(0)->save();
          $worry->addAnswerNum();
          $this->getUser()->setFlash('success','解答发布成功！');
          $this->redirect('worry/show?id='.$worry_id);
      }
  }
  
  
  public function executeCreate(sfWebRequest $request)
  {
    if($request->isMethod('POST')){
        $title = trim($request->getParameter('title',''));
        $content = trim($request->getParameter('content',''));
        
        if(strlen($title) == 0){
            $this->redirect('worry/index');
        }
        
        $user_id = $this->getUser()->getAttribute('user_id');
        $worry_obj = new Worry();
        $worry_obj->setUserId($user_id)->setTitle($title)->setContent($content)->save();
        $this->getUser()->setFlash('success','发布成功！');
        $this->redirect('worry/show?id='.$worry_obj->getId());
    }
  }
  
  public function executeShow(sfWebRequest $request){
      $this->forward404Unless($this->worry = Doctrine::getTable('Worry')->findOneById($id = $request->getParameter('id',0)));
      $this->getResponse()->setTitle($this->worry->getTitle().' - 烦恼 - 壹童学');
      if($this->worry->getDisplay() == 0) $this->redirect ('worry/index');
  }
  
  public function executeAttention(sfWebRequest $request){
      if($request->isXmlHttpRequest()){
          $user_id = $this->getUser()->getAttribute('user_id',0);
          $worry_id = (int)$request->getParameter('worry_id',0);
          $return_msg = array('error'=>0,'msg'=>'');
          
          if(!$this->getUser()->isAuthenticated()){
              $return_msg['error'] = 1;
              $return_msg['msg'] = '请先登入！';
              return $this->renderText(json_encode($return_msg));
          }
          
          $is_attention = Doctrine::getTable('WorryAttention')->createQuery()->where('worry_id =?',$worry_id)->andWhere('user_id = ?',$user_id)->execute();
          
          if($is_attention->count() != 0){ 
              $return_msg['error'] = 1;
              $return_msg['msg'] = '您已经有了这个烦恼，过多不好！';
              return $this->renderText(json_encode($return_msg));
          }
          
          $worry = Doctrine::getTable('Worry')->findOneById($worry_id);
          if(!$worry) return $this->renderText(json_encode($return_msg));
          
          if($worry->getUserId() == $user_id){ //debug
              $return_msg['error'] = 1;
              $return_msg['msg'] = '您是作者，不能自己关注自己的烦恼！';
              return $this->renderText(json_encode($return_msg));
          }
          
          $worry_attention = new WorryAttention();
          $worry_attention->setUserId($user_id)->setWorryId($worry_id)->setIsAuthor(0)->save();
          $worry->addAttentionNum();
          $return_msg['error'] = 0;
          $return_msg['msg'] = '关注成功！';
          $return_msg['data'] = array();
          $return_msg['data']['people_num'] = $worry->getAttentionNum();
          $return_msg['data']['people'] = Doctrine::getTable('WorryAttention')->getPersonCoverToArray($worry_id);
          
          return $this->renderText(json_encode($return_msg));
      }
  }
  
  public function executeUnattention(sfWebRequest $request){
      if($request->isXmlHttpRequest()){
          $user_id = $this->getUser()->getAttribute('user_id',0);
          $worry_id = (int)$request->getParameter('worry_id',0);
          $return_msg = array('error'=>0,'msg'=>'');
          
          if(!$this->getUser()->isAuthenticated()){
              $return_msg['error'] = 1;
              $return_msg['msg'] = '请先登入！';
              return $this->renderText(json_encode($return_msg));
          }
          
          $is_attention = Doctrine::getTable('WorryAttention')->createQuery()->where('worry_id =?',$worry_id)->andWhere('user_id = ?',$user_id)->execute();
          
          if($is_attention->count() == 0){ 
              $return_msg['error'] = 1;
              $return_msg['msg'] = '您没有关注过此烦恼哦！';
              return $this->renderText(json_encode($return_msg));
          }
          
          $worry = Doctrine::getTable('Worry')->findOneById($worry_id);
          if(!$worry) return $this->renderText(json_encode($return_msg));
          
          if($worry->getUserId() == $user_id){ //debug
              $return_msg['error'] = 1;
              $return_msg['msg'] = '您是作者，不能取消关注自己的烦恼！';
              return $this->renderText(json_encode($return_msg));
          }
          
          $is_attention->delete();
          $worry->decreaseAttentionNum();
          $return_msg['error'] = 0;
          $return_msg['msg'] = '恭喜您减轻了一个烦恼！';
          $return_msg['data'] = array();
          $return_msg['data']['people_num'] = $worry->getAttentionNum();
          $return_msg['data']['people'] = Doctrine::getTable('WorryAttention')->getPersonCoverToArray($worry_id);
          
          return $this->renderText(json_encode($return_msg));
      }
  }
  
}
